<template>
	<view class="select-city-pop">
		<u-popup :show="show" mode="bottom" :round="20" bgColor="white" @close="show = false">
			<view class="select-city-pop-box">
				<view class="select-city-pop-header">
					<view class="select-city-pop-title">请选择城市</view>
					<image class="select-city-pop-header-close" src="/static/alert_close_icon.png" mode="aspectFit" @tap="show = false"></image>
				</view>
				<xh-segment :titles="titles" :selectIndex="selectIndex" @didSelectIndex="didSelectItem"></xh-segment>
				<scroll-view class="scroll-view" :scroll-y="true" >
					<view class="list-box">
						<view class="group" v-for="(item,index) in cityList" :key="index">
							<view class="group-item" v-for="(model,i) in item" :key="model.code" @tap="didSelectCity(model)">
								<view class="group-item-letter">{{ i == 0 ? letterList[index] : ''}}</view>
								<text class="group-item-name">{{model.name}}</text>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</u-popup>
	</view>
</template>

<script setup>
	import { ref } from 'vue'
	import { useCityStore,useFormStore } from '@/stores'
	const props = defineProps({
		///是否可以选择区县
		isArea:false
	})
	//表单数据仓库
	const formStore = useFormStore()
	//城市数据仓库
	const cityStore = useCityStore()
	// 是否显示选择城市弹框
	const show = ref(false) 
	///城市数据
	const cityList = ref([])
	//字母数组
	const letterList = ref([])
	//选择的城市数据
	const selectCitys = ref([])
	///选择提示组件内容数组
	const titles = ref(['选择省份'])
	///选择提示组件当前下标
	const selectIndex = ref(0)
	//显示
	const open = () => {
		show.value = true
		selectCitys.value = []
		titles.value = ['选择省份']
		selectIndex.value = 0
		getCitysData()
	}
	//关闭
	const close = () => {
		show.value = false
	}
	//获取城市数据
	const getCitysData = ()=>{
		formatLetterGroup(cityStore.cityList)
	}
	//城市分组处理
	const formatLetterGroup = (list)=>{
		if(list.length > 0) {
			const count = list.length
			cityList.value = []
			let letters = []
			for (let i = 0;i< count;i++) {
				letters.push(list[i].pyName)
			}
			const resLetters = Array.from(new Set(letters))
			letterList.value = resLetters.sort()
			letterList.value.forEach((letter,i)=>{
				let datas = []
				list.forEach((item,j)=>{
					if (item.pyName == letter){
						datas.push(item)
					}
				})
				cityList.value.push(datas)
			})
		}
		else{
			cityList.value = []
		}
	}
	//选择城市
	const didSelectCity = (item)=>{
		if (selectCitys.value.length == 0) {
			//选择省份
			formatLetterGroup(item.cityList || [])
			selectCitys.value.push(item)
			updateSegmentStatus()
		}
		else if (selectCitys.value.length == 1){
			//选择城市
			if (!props.isArea){
				///银行卡选择城市，不支持选择区县
				selectCitys.value.push(item)
				formStore.provinceName = selectCitys.value[0].name
				formStore.provinceCode = selectCitys.value[0].code
				formStore.cityName = selectCitys.value[1].name
				formStore.cityCode = selectCitys.value[1].code
				show.value = false
			}
			else{
				console.log(item)
				///经营区域选择，支持选择区县
				formatLetterGroup(item.cityList || [])
				selectCitys.value.push(item)
				updateSegmentStatus()
			}
		}
		else if (selectCitys.value.length == 2){
			//选择区县
			selectCitys.value.push(item)
			formStore.storeProvinceName = selectCitys.value[0].name
			formStore.storeCityName = selectCitys.value[1].name
			formStore.storeAreaName = selectCitys.value[2].name
			show.value = false
		}
		console.log(item)
	}
	//更新顶部选择提示组件状态
	const updateSegmentStatus = ()=>{
		if (selectCitys.value.length == 0) {
			titles.value = ['选择省份']
			selectIndex.value = 0
		}
		else if (selectCitys.value.length == 1) {
			const model = selectCitys.value[0]
			titles.value = [model.name,'选择城市']
			selectIndex.value = 1
		}
		else if (selectCitys.value.length == 2) {
			const model1 = selectCitys.value[0]
			const model2 = selectCitys.value[1]
			titles.value = [model1.name,model2.name,'选择区县']
			selectIndex.value = 2
		}
	}
	//选择顶部选择提示组件项
	const didSelectItem = (index)=>{
		if (index >= selectCitys.value.length){
			return
		}
		if (index == 0){
			formatLetterGroup(cityStore.cityList)
			selectCitys.value = []
			updateSegmentStatus()
		}
		else if (index == 1) {
			const model1 = selectCitys.value[0]
			const model2 = selectCitys.value[selectCitys.value.length - 1]
			formatLetterGroup(model1.cityList)
			selectCitys.value.pop()
			updateSegmentStatus()
		}
		
	}
	// 暴露子组件的属性/方法给外界调用
	defineExpose({
		open,
		close
	})
</script>

<style lang="scss" scoped>
.select-city-pop-box{
	background-color: white;
	padding: 30rpx;
	border-top-left-radius: 20rpx;
	border-top-right-radius: 20rpx;
	height: 1000rpx;
	.select-city-pop-header{
		display: flex;
		align-items: center;
		justify-content: center;
		position: relative;
		.select-city-pop-title{
			text-align: center;
			font-size: 36rpx;
			color: $xh-title-color;
		}
		.select-city-pop-header-close{
			width: 40rpx;
			height: 40rpx;
			position: absolute;
			right: 10rpx;
		}
		margin-bottom: 40rpx;
	}
	
	.scroll-view{
		width: 100%;
		height: 840rpx;
		background-color: white;
		.group-item{
			display: flex;
			align-items: center;
			padding: 20rpx 0; 
			font-size: 28rpx;
			color: $xh-title-color;
			.group-item-letter{
				color: $xh-subtitle-color;
				margin-right: 20rpx;
				width: 30rpx;
			}
			
		}
	}
}
</style>